package view;


import conponent.*;
import module.EntityType;
import module.Position;
import module.StaticSources;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CombatView extends View implements KeyListener,Runnable {
    private Image bg;
    private Player p1,p2;
    private List<Entity> etl;

    /**
     * 在构造函数中完成视图数据的初始化
     */
    public CombatView(){
        this.addKeyListener(this);
        this.etl=new ArrayList<>();
        this.init();

    }

    public Player getPlayer01(){
        return p1;
    };

    /**
     * 战斗地图初始化
     */
    private void init(){
        SimpleEntityFactory f = new SimpleEntityFactory();
        //添加玩家对象
        p1=f.createPlayer01();
        addEntityComp((EntityComp) p1);
        //添加随机的树
        Random r = new Random();
        for(int i=0;i<30;i++){
            addEntityComp(f.createTree(r.nextInt(25),r.nextInt(19)));
        }
        //启动视图线程
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    /**
     * 视图的渲染工作
     */
    public void paint(Graphics g) {
        Random r = new Random();
        //绘制非实体地图
        for(int i=0;i<26;i++){
            //高
            for (int j=0;j<20;j++){
                g.drawImage(StaticSources.i00,30*i,30*j,32,32,this);
            }
        }

        Position pos = p1.getPosition();

        //绘制地图实体，包括人物和障碍物
        if(etl!=null){
            for(Entity et:etl){
                Position p = et.getPosition();
                if(et.getEntityType()==EntityType.Player){
//                    System.out.println(p.X+","+p.Y);
                }

                //对实体是魔法球的特殊处理
                if(et.getEntityType()== EntityType.MagicBall){
                    MagicBall mb=(MagicBall) et;
//                    mb.beforeCreated();
//                    System.out.println("point A");
                }
                g.drawImage(et.disPlay(), p.X, p.Y, p.W, p.H, this);
            }
        }
        g.drawImage(StaticSources.dirk,pos.X+20,pos.Y,25,25,this);
    }



    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode){
            case KeyEvent.VK_S : {
                p1.downMove();
                break;
            }
            case KeyEvent.VK_D : {
                p1.rightMove();
                break;
            }
            case KeyEvent.VK_A : {
                p1.leftMove();
                break;
            }
            case KeyEvent.VK_W : {
                p1.upMove();
                break;
            }
            case KeyEvent.VK_F : {
                p1.action(0);
                break;
            }
            default:break;

        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode==KeyEvent.VK_D||keyCode==KeyEvent.VK_S||keyCode==KeyEvent.VK_W||keyCode==KeyEvent.VK_A){
            p1.stopMove();
        }

    }

    @Override
    public List<Entity> getEntityList() {
        return etl;
    }

    public void addEntityComp(Entity et){
        etl.add(et);
        et.setLoc(this);
    }

    @Override
    public void run() {
        while(true){

            this.repaint();
            try {
                Thread.sleep(125);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
